<%@ page import="java.sql.*" %>
<%@ page import="dbpool.*" %>
<%--
  Created by IntelliJ IDEA.
  User: 86147
  Date: 2024/4/14
  Time: 17:29
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>处理用户注册信息添加到数据库bookstore的user表（用户注册）</title>
</head>
<body>
<%
    request.setCharacterEncoding("UTF-8");
    String username = request.getParameter("username");
    String password = request.getParameter("password");
    Connection conn = null;
    Statement stmt = null;
   // String url = "jdbc:mysql://localhost:3306/bookstore?useUnicode=true&characterEncoding=utf-8";
    //1 加载JDBC驱动
    try{
        //Class.forName("com.mysql.jdbc.Driver");
        conn = DbcpConnectionPool.getConnection();
        //2 建立与数据库的连接
       // conn = DriverManager.getConnection(url,"root","111111");
        // 创建操作数据库SQL的Statement对象
        //将注册页面的信息传入数据库的user表中进行存储
        String sql = "insert into user (username,password) values ('"+username+"','"+password+"')";
        stmt = conn.createStatement();
        //4 执行SQL语句,并分析执行结果
        int num = stmt.executeUpdate(sql);
        if(num>0){
            // 插入记录成功
            // 注册成功后跳转到注册成功界面
            response.sendRedirect("registersuccess.jsp");
        }
    } catch (Exception e){
        e.printStackTrace();
    }finally {
        // 5. 关闭连接，释放资源
        if (stmt!=null){
            try{
                stmt.close();
            }catch (SQLException e){
                e.printStackTrace();
            }
            stmt = null;
        }
        if (conn!=null){
            try{
                conn.close();
            }catch (SQLException e){
                e.printStackTrace();
            }
            conn = null;
        }
    }
%>
</body>
</html>
